﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.IO;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Execution;

namespace ICeFramework.Utilities.VisualStudio
{
    public static class VisualStudioIDE
    {
        public static List<VisualStudioIDEProperties> GetInstalledVersions()
        {
            List<VisualStudioIDEProperties> list = new List<VisualStudioIDEProperties>();
            VisualStudioIDEProperties prop = GetIDEPropertiesByVersion("10.0");
            if (prop != null)
            {
                list.Add(prop);
            }
            prop = GetIDEPropertiesByVersion("9.0");
            if (prop != null)
            {
                list.Add(prop);
            }

            prop = GetIDEPropertiesByVersion("8.0");
            if (prop != null)
            {
                list.Add(prop);
            }

            prop = GetIDEPropertiesByVersion("7.1");
            if (prop != null)
            {
                list.Add(prop);
            }
            return list;
        }

        private static VisualStudioIDEProperties GetIDEPropertiesByVersion(string version)
        {
            VisualStudioIDEProperties result = null;
            string vsKey = string.Format(@"SOFTWARE\Microsoft\VisualStudio\{0}\Setup\VS", version);
            using (RegistryKey regKey = Registry.LocalMachine.OpenSubKey(vsKey))
            {
                try
                {
                    if (regKey != null)
                    {
                        string name = string.Empty;
                        switch (version)
                        {
                            case "7.1":
                                name = "Visual Studio 2003";
                                break;
                            case "8.0":
                                name = "Visual Studio 2005";
                                break;
                            case "9.0":
                                name = "Visual Studio 2008";
                                break;
                            case "10.0":
                                name = "Visual Studio 2010";
                                break;
                        }
                        if (!string.IsNullOrEmpty(name))
                        {
                            string envPath = regKey.GetValue("EnvironmentPath") != null ? regKey.GetValue("EnvironmentPath").ToString() : null;
                            if (string.IsNullOrEmpty(envPath))
                            {
                                return null;
                            }
                            result = new VisualStudioIDEProperties(name, version);
                            result.EnvironmentPath = regKey.GetValue("EnvironmentPath").ToString();
                        }
                    }
                }
                finally
                {
                    if (regKey != null)
                    {
                        regKey.Close();
                    }
                }
            }
            return result;
        }

        public static List<string> GetProjectTargets(string projectFile)
        {
            List<string> targetList = new List<string>();

            if (!File.Exists(projectFile))
            {
                return targetList;
            }

            ProjectCollection projects = ProjectCollection.GlobalProjectCollection;
            Project project = projects.LoadProject(projectFile);

            foreach (string prop in project.ConditionedProperties.Keys)
            {
                //Console.WriteLine(string.Format("{0} = {1}", project.ConditionedProperties[prop]));
            }

            foreach (KeyValuePair<string, string> gp in project.GlobalProperties)
            { 

            }

            foreach (KeyValuePair<string, ProjectTargetInstance> target in project.Targets)
            { 
                //target.Value.
            }

            projects.UnloadProject(project);

            return targetList;
        }
    }
}
